package cn.luoshaocheng.org.service.impl;

import cn.luoshaocheng.basic.service.impl.BaseServiceImpl;
import cn.luoshaocheng.org.domain.Employee;
import cn.luoshaocheng.org.mapper.EmployeeMapper;
import cn.luoshaocheng.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Employee employee) {
        //保存employee表
        employeeMapper.save(employee);
        // 保存中间表
        employeeMapper.saveEmployeeRole(employee.getId(),employee.getRole_id());
    }

    @Override
    public void delete(Long eid) {
        //先删除中间表 直接删除自己删除不了
        employeeMapper.removeEmployeeRole(eid);

        employeeMapper.remove(eid);
    }

    @Override
    public void update(Employee employee) {
        super.update(employee);
        // 根据id删除t_employee_role
        employeeMapper.removeEmployeeRole(employee.getId());
        //在加一波
        employeeMapper.saveEmployeeRole(employee.getId(),employee.getRole_id());
    }

    @Override
    public List<Employee> findByShopId(Long shopId) {
        return employeeMapper.findByShopId(shopId);
    }
}

